package com.jinzhi.jzweb.controller;


import java.util.*;

import com.jinzhi.jzweb.domain.BookStockDO;
import com.jinzhi.jzweb.pojo.BookPurchaseVo;
import com.jinzhi.jzweb.service.BookStockService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.BookPurchaseDO;
import com.jinzhi.jzweb.service.BookPurchaseService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 书籍进货管理表
 * </pre>
 * <small> 2021-04-08 09:00:20 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/bookPurchase")
public class BookPurchaseController extends BaseController {
	@Autowired
	private BookPurchaseService bookPurchaseService;
	@Autowired
	private BookStockService bookStockService;
	@Autowired
	private UserService userService;
	@GetMapping()
	@RequiresPermissions("jzweb:bookPurchase:bookPurchase")
	String BookPurchase(){
	    return "jzweb/bookPurchase/bookPurchase";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:bookPurchase:bookPurchase")
	public Result<Page<BookPurchaseDO>> list(BookPurchaseDO bookPurchaseDTO){
		// 查询列表数据
		Page<BookPurchaseDO> page = bookPurchaseService.selectPage(getPage(BookPurchaseDO.class),
					bookPurchaseService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				BookPurchaseDO obj = (BookPurchaseDO) page.getRecords().get(i);

			BookPurchaseVo bookPurchaseVo=new BookPurchaseVo();
			bookPurchaseVo.setPurchaseTime(obj.getPurchaseTime());
			bookPurchaseVo.setId(obj.getId());
			bookPurchaseVo.setPurchaseNumber(obj.getPurchaseNumber());
			//查询操作人
			UserDO userDO=userService.selectById(obj.getPurchaseUser());
			bookPurchaseVo.setPurchaseUser(userDO.getName());
			//查询书籍名称
			BookStockDO bookStockDO=bookStockService.selectById(obj.getBookId());
			if(bookStockDO!=null){
				bookPurchaseVo.setBookNmae(bookStockDO.getBookType());
			}

			bookPurchaseVo.setRemarks(obj.getRemarks());
			bookPurchaseVo.setBookTypes(obj.getBookTypes());
			list.add(bookPurchaseVo);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:bookPurchase:add")
	String add(Model model){
		Map map=new HashMap();
		List<BookStockDO> list=bookStockService.list(map);
		model.addAttribute("list",list);
		List <Map> mapList=new ArrayList<>();

		Map map1=new HashMap();
		map1.put("name","幼儿照护");
		mapList.add(map1);
		Map map2=new HashMap();
		map2.put("name","产后恢复");
		mapList.add(map2);
		model.addAttribute("list2",mapList);

	    return "jzweb/bookPurchase/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:bookPurchase:edit")
	String edit(@PathVariable("id") Long id,Model model){
		BookPurchaseDO bookPurchase = bookPurchaseService.selectById(id);
		model.addAttribute("bookPurchase", bookPurchase);
		Map map=new HashMap();
		List<BookStockDO> list=bookStockService.list(map);
		model.addAttribute("list",list);
		List <Map> mapList=new ArrayList<>();

		Map map1=new HashMap();
		map1.put("name","幼儿照护");
		mapList.add(map1);
		Map map2=new HashMap();
		map2.put("name","产后恢复");
		mapList.add(map2);
		model.addAttribute("list2",mapList);
	    return "jzweb/bookPurchase/edit";
	}
	
	@Log("添加书籍进货管理表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:bookPurchase:add")
	public Result<String> save( BookPurchaseDO bookPurchase){
		bookPurchase.setPurchaseTime(new Date());
		bookPurchase.setPurchaseUser(this.getUserId());
		bookPurchaseService.insert(bookPurchase);
        return Result.ok();
	}
	
	@Log("修改书籍进货管理表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:bookPurchase:edit")
	public Result<String>  update( BookPurchaseDO bookPurchase){
		boolean update = bookPurchaseService.updateById(bookPurchase);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除书籍进货管理表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:bookPurchase:remove")
	public Result<String>  remove( Long id){
		bookPurchaseService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除书籍进货管理表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:bookPurchase:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		bookPurchaseService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
